﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    public class OperatorDayPosition
    {
        public int Id { get; set; }

        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        public int AccountId { get; set; }

        public virtual Sys.Account Account { get; set; }

        /// <summary>
        /// 合约
        /// </summary>
        public string InstrumentId { get; set; }
        /// <summary>
        /// 经纪公司代码
        /// </summary>
        public string Brokerid { get; set; }
        /// <summary>
        /// 投资者代码
        /// </summary>
        public string Investorid { get; set; }
        /// <summary>
        /// 投保
        /// </summary>
        public string HedgeFlag { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public string Direction { get; set; }
        /// <summary>
        /// 开仓时间
        /// </summary>
        public int OpenTime { get; set; }
        /// <summary>
        /// 开仓日期
        /// </summary>
        public int OpenDate { get; set; }
        /// <summary>
        /// 成交编号
        /// </summary>
        public string TradeId { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public int Volume { get; set; }
        /// <summary>
        /// 开仓价
        /// </summary>
        public double OpenPrice { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public string TradingDay { get; set; }
        /// <summary>
        /// 结算编号
        /// </summary>
        public int SettleMentId { get; set; }
        /// <summary>
        /// 成交类型
        /// </summary>
        public string TradeType { get; set; }
        /// <summary>
        /// 组合合约代码
        /// </summary>
        public string CombinStrumentId { get; set; }
        /// <summary>
        /// 交易所id
        /// </summary>
        public string ExchangeId { get; set; }
        /// <summary>
        /// 逐日盯市平仓盈亏
        /// </summary>
        public double CloseProfitByDate { get; set; }
        /// <summary>
        /// 逐笔对冲平仓盈亏
        /// </summary>
        public double CloseProfitByTrade { get; set; }
        /// <summary>
        /// 逐日盯市持仓盈亏
        /// </summary>
        public double PositionProfitByDate { get; set; }
        /// <summary>
        /// 逐笔对冲持仓盈亏
        /// </summary>
        public double PositionProfitByTrade { get; set; }
        /// <summary>
        /// 保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 交易所保证金
        /// </summary>
        public double Exchmargin { get; set; }
        /// <summary>
        /// 保证金率
        /// </summary>
        public double MarginRateByMoney { get; set; }
        /// <summary>
        /// 保证金率(按手数)
        /// </summary>
        public double MarginRateByVolume { get; set; }
        /// <summary>
        /// 昨结算
        /// </summary>
        public double YesterdaySettlement { get; set; }
        /// <summary>
        /// 结算价
        /// </summary>
        public double SettleMentPrice { get; set; }
        /// <summary>
        /// 平仓量
        /// </summary>
        public int CloseVolume { get; set; }
        /// <summary>
        /// 平仓金额
        /// </summary>
        public double CloseAmount { get; set; }
        /// <summary>
        /// 品种
        /// </summary>
        public string ProductId { get; set; }
        /// <summary>
        /// 浮动盈亏
        /// </summary>
        public double FloatingProfit { get; set; }
        /// <summary>
        /// 盯市盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }

        /// <summary>
        /// 这个方法用的不对，应该从持仓明细里面去读，这样就有手数和成交价了
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="operatorId"></param>
        /// <returns></returns>
        public List<View.OperatorDayPosition> GetAllAccountDayPositionDelivery(int tradeHistoryId, int operatorId, DB.DBManager db)
        {
            var query = db.OperatorDayPosition.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Volume > 0);
            return query.ToList();
        }

        /// <summary>
        /// 初始化用户持仓信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void InitOperatorDayPosition(int tradeHistoryId, DB.DBManager db)
        {
            var operators = new Sys.Operator().GetUnDeletedOperators(db);
            foreach (Sys.Operator op in operators)
            {
                List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), op.Id, db);
                foreach (Sys.Account account in accounts)
                {
                    List<Data.Day.AccountDayPositionDelivery> list = new Data.Day.AccountDayPositionDelivery().GetAccountDayPositionDelivery(account.Id, tradeHistoryId, db);
                    foreach (Data.Day.AccountDayPositionDelivery s in list)
                    {
                        OperatorDayPosition change = new OperatorDayPosition();
                        change.OperatorId = op.Id;
                        change.Brokerid = s.Brokerid;
                        change.AccountId = s.AccountId;
                        change.CloseAmount = s.CloseAmount;
                        change.ExchangeId = s.ExchangeId;
                        change.Exchmargin = s.Exchmargin;
                        change.CloseProfitByDate = s.CloseProfitByDate;
                        change.CloseProfitByTrade = s.CloseProfitByTrade;
                        change.CloseVolume = s.CloseVolume;
                        change.CombinStrumentId = s.CombinStrumentId;
                        change.Commission = s.Commission;
                        change.Direction = s.Direction;
                        change.FloatingProfit = s.FloatingProfit;
                        change.HedgeFlag = s.HedgeFlag;
                        change.InstrumentId = s.InstrumentId;
                        change.Investorid = s.Investorid;
                        change.Margin = s.Margin;
                        change.MarginRateByMoney = s.MarginRateByMoney;
                        change.MarginRateByVolume = s.MarginRateByVolume;
                        change.OpenDate = s.OpenDate;
                        change.OpenPrice = s.OpenPrice;
                        change.OpenTime = s.OpenTime;
                        change.PositionProfit = s.PositionProfit;
                        change.PositionProfitByDate = s.PositionProfitByDate;
                        change.PositionProfitByTrade = s.PositionProfitByTrade;
                        change.ProductId = s.ProductId;
                        change.SettleMentPrice = s.SettleMentPrice;
                        change.TradeHistoryId = tradeHistoryId;
                        change.TradeId = s.TradeId;
                        change.TradeType = s.TradeType;
                        change.TradingDay = s.TradingDay;
                        change.Volume = s.Volume;
                        change.YesterdaySettlement = s.YesterdaySettlement;
                        db.OperatorDayPosition.Add(change);
                    }
                }
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除某日用户持仓信息
        /// </summary>
        /// <param name="tradeHistory"></param>
        /// <param name="db"></param>
        public void Delete(int tradeHistory, DB.DBManager db)
        {
            var query = db.OperatorDayPosition.Where(a => a.TradeHistoryId == tradeHistory);
            foreach (OperatorDayPosition p in query)
            {
                db.OperatorDayPosition.Remove(p);
            }
            db.SaveChanges();
        }
    }
}
